package mspbots.timedoctor.client.core.api.v2;

import lombok.extern.log4j.Log4j2;
import mspbots.timedoctor.client.core.api.BaseApiService;
import mspbots.timedoctor.client.core.api.ResultMessageModel;
import mspbots.timedoctor.client.core.api.constant.API;
import mspbots.timedoctor.client.core.listener.SyncTimeDTO;
import mspbots.timedoctor.common.RabbitKeys;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * data.connector.timedoctor.api.CompaniesService
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2020/3/25
 */
@Log4j2
@Service
public class CompaniesV2Service extends BaseApiService {


    public void async(String sync_id, Long tenant, String companyId) {
        final long start = System.nanoTime();
        this.doctorV2Client.get(API.V2_COMPANIES + "/" + companyId, Map.of("tenantId", tenant))
                .map(jsonNode -> jsonNode.get("data"))
                .subscribe(result -> {
                            this.messagingTemplate.convertAndSend(RabbitKeys.BASE_EXCHANGE_NAME,
                                    RabbitKeys.DISTRIBUTED_TASK_QUEUE_KEY,
                                    ResultMessageModel.builder().action(ResultMessageModel.MessageModelAction.ADD)
                                            .type(ResultMessageModel.MessageModelType.COMPANIES)
                                            .sendTime(LocalDateTime.now()).entity(result).tenantId(tenant).build());
                            long milliseconds = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
                            log.debug("Time taken {}: {}  milliseconds.", "Completed", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
                            this.messagingTemplate.convertAndSend("timedoctor.sync.time", SyncTimeDTO.builder()
                                    .api(API.V2_COMPANIES)
                                    .last_time(LocalDateTime.now())
                                    .milliseconds(milliseconds)
                                    .status("Completed")
                                    .msg("Success")
                                    .tenantId(tenant)
                                    .build()
                            );

                            this.sendLog(sync_id, tenant, milliseconds, API.V2_COMPANIES, SUCCESS_CODE, "", result);
                        }
                        , err -> {
                            long milliseconds = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
                            log.error("Companies sync data error, msg:{}", err.getMessage());
                            this.messagingTemplate.convertAndSend("timedoctor.sync.time", SyncTimeDTO.builder()
                                    .api(API.V2_COMPANIES)
                                    .last_time(LocalDateTime.now())
                                    .milliseconds(milliseconds)
                                    .status("Error")
                                    .msg(err.getMessage())
                                    .tenantId(tenant)
                                    .build()
                            );

                            this.sendLog(sync_id, tenant, milliseconds, API.V2_COMPANIES, FAIL_CODE, err.getMessage(), null);
                        });
    }


}
